# @file    : test1
# @time    : 2025/4/15
# @author  : yongpeng.yao
# @desc    :
import base64
import os

from dotenv import load_dotenv
from openai import OpenAI

load_dotenv()

client = OpenAI(
    api_key=os.getenv("OPENAI_API_KEY"),
    base_url="https://qianfan.baidubce.com/v2",
)


# Function to encode the image
def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode("utf-8")


# Path to your image
image_path = "lc1.png"

# Getting the Base64 string
base64_image = encode_image(image_path)

response = client.chat.completions.create(
    model="ernie-4.5-8k-preview",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "这张图片有什么？",
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": f"data:image/pngs;base64,{base64_image}"
                    },
                },
            ],
        }
    ],
    stream=True
)

for chunk in response:
    if chunk.choices[0].delta.content is not None:
        print(chunk.choices[0].delta.content, end="")
